package web.kx;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLConnection;

import org.htmlparser.http.ConnectionManager;

public class Cookie {
	public void t(URLConnection conn){
		// Create a URLConnection object for a URL
		// Get all cookies from the server.
		// Note: The first call to getHeaderFieldKey() will implicit send
		// the HTTP request to the server.
		for (int i=0; ; i++) {
		    String headerName = conn.getHeaderFieldKey(i);
		    String headerValue = conn.getHeaderField(i);

		    if (headerName == null && headerValue == null) {
		        // No more headers
		        break;
		    }
		    if ("Set-Cookie".equalsIgnoreCase(headerName)) {
		        // Parse cookie
		        String[] fields = headerValue.split(";\\s*");

		        String cookieValue = fields[0];
		        String expires = null;
		        String path = null;
		        String domain = null;
		        boolean secure = false;

		        // Parse each field
		        for (int j=1; j<fields.length; j++) {
		            if ("secure".equalsIgnoreCase(fields[j])) {
		                secure = true;
		            } else if (fields[j].indexOf('=') > 0) {
		                String[] f = fields[j].split("=");
		                if ("expires".equalsIgnoreCase(f[0])) {
		                    expires = f[1];
		                } else if ("domain".equalsIgnoreCase(f[0])) {
		                    domain = f[1];
		                } else if ("path".equalsIgnoreCase(f[0])) {
		                    path = f[1];
		                }
		                Main.logger.info(f[0]+"xxxxxx"+f[1]);
		                new org.htmlparser.http.Cookie(f[0],f[1]);
		                ConnectionManager cm = null;
		            }
		        }

		    }
		}
 
	}
}
